# frozen_string_literal: true

require 'spec_helper'
require 'email_spec'

RSpec.describe Emails::MergeRequests, feature_category: :code_review_workflow do
  include EmailSpec::Matchers

  # rubocop: disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:current_user) { create(:user, name: 'Jane Doe') }
  let_it_be(:reviewer) { create(:user) }
  let_it_be(:merge_request) { create(:merge_request, author: current_user, reviewers: [reviewer]) }
  let_it_be(:merge_request_diff) { create(:merge_request_diff, merge_request: merge_request) }
  # rubocop: enable RSpec/FactoryBot/AvoidCreate

  describe '#changed_reviewer_of_merge_request_email' do
    let(:previous_reviewer_ids) { [] }

    subject do
      Notify.changed_reviewer_of_merge_request_email(
        reviewer.id,
        merge_request.id,
        previous_reviewer_ids,
        current_user.id,
        'review_requested'
      )
    end

    before do
      allow(::Llm::MergeRequests::SummarizeDiffService).to receive(:enabled?).and_return(summarize_diff_service_enabled)
    end

    context 'when SummarizeDiffService is enabled' do
      let(:summarize_diff_service_enabled) { true }

      context 'when merge_request_diff_llm_summary exists' do
        # rubocop: disable RSpec/FactoryBot/AvoidCreate
        let_it_be(:diff_summary) { create(:merge_request_diff_llm_summary, merge_request_diff: merge_request_diff) }
        # rubocop: enable RSpec/FactoryBot/AvoidCreate

        context 'when the recipient is a new reviewer' do
          it 'include a diff summary' do
            subject.body.parts.each do |part|
              expect(part.body).to have_content("#{current_user.name} requested a new review on")
              expect(part.body).to have_content(diff_summary.content)
              expect(part.body).to have_content("Summary generated by AI")
            end
          end
        end

        context 'when the recipient is not a new reviewer' do
          let(:previous_reviewer_ids) { [reviewer.id] }

          it 'does not include a diff summary' do
            subject.body.parts.each do |part|
              expect(part.body).not_to have_content("requested a new review on")
              expect(part.body).not_to have_content("Summary generated by AI")
            end
          end
        end
      end

      context 'when merge_request_diff_llm_summary does not exists' do
        it 'does not include a diff summary' do
          subject.body.parts.each do |part|
            expect(part.body).not_to have_content("requested a new review on")
            expect(part.body).not_to have_content("Summary generated by AI")
          end
        end
      end
    end

    context 'when SummarizeDiffService is disabled' do
      let(:summarize_diff_service_enabled) { false }

      it 'does not include a diff summary' do
        subject.body.parts.each do |part|
          expect(part.body).not_to have_content("requested a new review on")
          expect(part.body).not_to have_content("Summary generated by AI")
        end
      end
    end
  end
end
